package com.xiaotu.spider.qq.pageprocessor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVDanMuCommentModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * 爬取腾讯视频网大电影弹幕
 * @author wangyanlong
 * @date 2019年8月6日 上午9:26:14
 *
 */
@Component("QQMovieDanMuPageProcessor")
@Scope("prototype")
public class QQMovieDanMuPageProcessor extends APageProcessor
{
	
    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return new Request("https://v.qq.com/x/search/?q="
        		+KeywordEncode.encode(target.getString("tvName"))+"&stag=0&smartbox_ab=");
    }

    @Override
    public void process(Page page)
    {
        Map<String, Object> tvMap = page.getRequest().getExtras();
        switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page, tvMap);
                break;
            case Constants.PageDataType.TV_RECORD:
                this.tvProcess(page,tvMap);
                break;
            default:
                break;
        }
    }

    private void searchListProcess(Page page, Map<String, Object> tvMap){
    	List<String> divList = page.getHtml()
				.xpath("//div[@class='wrapper_main']/div")
				.all();
		if (divList == null || divList.isEmpty())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		for (String divStr : divList){
            Html divObj = new Html(divStr);
            String url = divObj.xpath("//div[@class='_playlist']/div[@class='result_btn_line']/a[1]/@href").get();
            String tvName = StringUtils.join(divObj.xpath
                    ("//h2[@class='result_title']//a//em[@class='hl']//text()").all(), SepratorUtil.SEP_COMMA_EN);
            if(StringUtil.isNotEmpty(tvName)){
            	tvName = RegexUtils.regexChStr(tvName).trim();
            }
            boolean dirFlage = false;
            boolean actorFlage = false;
        	String tvType = divObj.xpath("//h2[@class='result_title']/a/span[@class='type']/text()").get();
            List<String> peopleList = divObj.xpath("//div[@class='result_info']/div").all();
            for (String peopleDiv: peopleList) {
				Html peopleObj = new Html(peopleDiv);
				String labels = peopleObj.xpath("//span[@class='label']/text()").get();
				if(StringUtil.isNotEmpty(labels)){
					labels =labels.replaceAll("　", "");
					if(labels.contains("主演")){
						List<String> actorList = peopleObj.xpath("//span[@class='content']/a").all();
						for (String actorDiv : actorList) {
							Html actorObj = new Html(actorDiv);
							String actor = actorObj.xpath("//a/text()").get();
							if(tvMap.get("actor").toString().contains(actor)){
								actorFlage =true;
								break;
							}
						}
					}
					
					if(labels.contains("导演")){
						List<String> directorList = peopleObj.xpath("//span[@class='content']/a").all();
						for (String directorDiv : directorList) {
							Html directorObj = new Html(directorDiv);
							String director = directorObj.xpath("//a/text()").get();
							if(tvMap.get("director").toString().contains(director)){
								dirFlage =true;
								break;
							}
						}
					}
				}
			}
            
            
            String tvOldName = tvMap.get("tvName")+"";
            if(StringUtil.isNotEmpty(tvOldName)){
            	tvOldName = RegexUtils.regexChStr(tvOldName).trim();
            }
            if(tvOldName.equals(tvName) 
            		&& tvMap.get("tvType").toString().equals(tvType) 
            		&& dirFlage && actorFlage){
            	 PageUtils.addTargetRequest(page, url, tvMap);
            }
        }
		
    }
    
    private void tvProcess(Page page, Map<String, Object> tvMap) {
    	
    	String vid = page.getUrl().toString();
    	vid = vid.substring(vid.lastIndexOf("/")+1).replace(".html", "");
    	//获取 targetId
    	String httpGetJSONP = HttpUtils.httpGetJSONP
    			("http://bullet.video.qq.com/fcgi-bin/target/regist?otype=json&vid="+vid);
   	 	if (StringUtils.isEmpty(httpGetJSONP)){
   	 		throw new SpiderException(SpiderExceptionCode.DataInvalideException);
   	 	}
   	 	httpGetJSONP = httpGetJSONP.substring(httpGetJSONP.lastIndexOf("targetid"),
		httpGetJSONP.lastIndexOf("}") + 1);
 		String targetId = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, httpGetJSONP);
 		
 		 int timestamp=15;
 		 List<TVDanMuCommentModel> list = new ArrayList<TVDanMuCommentModel>();;
		 while(true){
			 //获取弹幕信息
			 String httpDanMuJSONP = HttpUtils.httpGetJSONP
					 ("http://mfm.video.qq.com/danmu?timestamp="+timestamp+"&target_id="+targetId);
			 if (StringUtils.isEmpty(httpDanMuJSONP))
				 throw new SpiderException(SpiderExceptionCode.DataInvalideException);
			 JSONObject commentListObject = JSONObject.fromObject(httpDanMuJSONP);
			 JSONArray commentList = commentListObject.getJSONArray("comments");
			 if(null !=commentList && commentList.size() >0){
				 for (int i = 0; i < commentList.size(); i++){
		                JSONObject comment = commentList.getJSONObject(i);
		                String opername = comment.getString("opername");
		                if(StringUtils.isNotEmpty(opername)){
		                	opername = opername.replaceAll("[^(0-9a-zA-Z\u4e00-\u9fa5\\p{P}\r\n )]","");
		                }
		                String content = comment.getString("content");
		                if(StringUtils.isNotEmpty(content)){
		                	content = content.replaceAll("[^(0-9a-zA-Z\u4e00-\u9fa5\\p{P}\r\n )]","");
		                }
		                list.add(new TVDanMuCommentModel(tvMap.get("tvName")+"",
		                		comment.getString("commentid"),opername,content,comment.getString("uservip_degree"),
		                		comment.getString("timepoint"),comment.getString("upcount"),"腾讯视频"));
		            }
						
				 		timestamp +=30;
			 }else{
				 	break;
			 }
			 	
		 }
		 	page.putField(TVDanMuCommentModel.class.getName(), list);
    }
    
    @Override
    public String getSpiderType()
    {
        return Constants.SpiderTaskType.QQ_MOVIE_DANMU;
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
        if (url.contains("https://v.qq.com/x/search/?q="))
            return Constants.PageDataType.SEARCH_LIST;
        else if (url.contains("v.qq.com/x/cover"))
            return Constants.PageDataType.TV_RECORD;
        return Constants.PageDataType.TV_COMMENT;
    }

}
